/**
 * 
 */
package be.lreenaers.mirr0r.schema.components.SimpleDublinCore;

import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.Border;

import org.jdom.Element;

import be.lreenaers.mirr0r.components.utils.Utils;
import be.lreenaers.mirr0r.schema.components.GenericDocument;
import be.lreenaers.mirr0r.schema.components.JdomDocument;
import be.lreenaers.mirr0r.schema.interfaces.XmlElement;
import be.lreenaers.mirr0r.schema.interfaces.XmlObject;

import com.csvreader.CsvReader;

/**
 * A Dublin Core document.
 * @author Ludovic Reenaers
 * @since 1.0 
 * @version 1.0
 */
public class DCDocument extends GenericDocument {
	
	/**
	 * Default constructor.
	 * @since 1.0
	 */
	public DCDocument(){
		setUp();
	}
	private void setUp() {
		this.elements = new ArrayList<XmlElement>();
		this.setName("Simple Dublin Core");
		this.rootEl = new Element("descriptionSet");
		this.rootEl.setNamespace(Utils.DCX_NAMESPACE);
		this.rootEl.addNamespaceDeclaration(Utils.DC_NAMESPACE);
		this.doc = new JdomDocument(this.rootEl);
		
		this.panel = new JPanel();
		this.panel.setName(this.getName());
		this.scroll = new JScrollPane(this.panel);
		this.panel.setLayout(new BoxLayout(this.panel, BoxLayout.PAGE_AXIS));
		Border blackline = BorderFactory.createLineBorder(Color.black);

		this.panel.setBorder(blackline);
		
		DCXDescriptionElement dcxDesc = new DCXDescriptionElement();
		this.elements.add(dcxDesc);
	}
	@Override
	public XmlObject getXml(CsvReader reader) throws IOException {
		System.out.println("in DCDOcument.getXml(); size of elements: "+this.elements.size()+", should be 1\n");
		XmlElement el;
		Iterator<XmlElement> iter = this.elements.iterator();
		this.rootEl.removeContent();
		while(iter.hasNext()){
			el = iter.next();
			while(reader.readRecord()){
				System.out.println("on");
				Element ell = (Element)el.getXml(reader);
				
				this.rootEl.addContent((Element)ell.clone());
			}
		}
		return (JdomDocument)this.doc;
	}

}
